import { myLocalStorage } from '@/utils/storage'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/Login.vue')
    },
    {
      path: '/',
      redirect: '/main'
    },
    {
      path: '/main',
      name: 'main',
      redirect: '/main/user',
      component: () => import('@/views/Main/Main.vue'),
      children: [
        {
          path: '/main/user',
          component: () => import('@/views/Main/User/User.vue')
        },
        {
          path: '/main/dept',
          component: () => import('@/views/Main/Dept/Dept.vue')
        },
        {
          path: '/main/dept',
          component: () => import('@/views/Main/Dept/Dept.vue')
        },
        {
          path: '/main/client',
          component: () => import('@/views/Main/Client/Client.vue')
        }
      ]
    },
    {
      path: '/:callAll(.*)',
      redirect: '/'
    }
  ]
})

// 导航守卫
router.beforeEach((to, _form, next) => {
  const token = myLocalStorage.getStorage('token')
  if (to.path.startsWith('/main') && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
